package com.ews.onlineexchange.service;

import java.util.List;

import javax.enterprise.inject.Model;
import javax.inject.Inject;
import javax.persistence.EntityManager;

import org.apache.lucene.search.Query;
import org.hibernate.search.jpa.FullTextEntityManager;
import org.hibernate.search.jpa.FullTextQuery;
import org.hibernate.search.query.dsl.QueryBuilder;

import com.ews.onlineexchange.model.ServiceSupplier;

@Model
public class ServiceSupplierFTList extends FullTextEntityQuery<ServiceSupplier> {
	
	private static final long serialVersionUID = 1L;

	@Inject
	EntityManager entityManager;
	
	private String searchString ;	
	private Integer auditStatus;
	
	public ServiceSupplierFTList() {
		super();
		setMaxResults(super.MAX_RESULTS_NORMAL);
	}

	@Override
	public List<ServiceSupplier> getResultList() {
		List<ServiceSupplier> results = createQuery().getResultList();
		return results;
	}
	
	public String getSearchString() {
		return searchString;
	}

	public void setSearchString(String searchString) {
		this.searchString = searchString;
	}

	@Override
	protected FullTextQuery createQuery() {
		FullTextEntityManager ftem = getFullTextEntityManager();
		return ftem.createFullTextQuery(createLuceneQuery(), ServiceSupplier.class);
	}

	@Override
	protected Query createLuceneQuery() {
		String searchQuery = this.searchString;
		FullTextEntityManager ftem = getFullTextEntityManager();
		QueryBuilder supplierQb = ftem.getSearchFactory().buildQueryBuilder().forEntity(ServiceSupplier.class).get();
		Query auditted = supplierQb.keyword().onField("auditted").matching(true).createQuery();
		Query supplierQuery = supplierQb.keyword().filteredBy(auditted).onFields("cnname").matching(searchQuery).createQuery();
//		
//		org.apache.lucene.search.Query combineQuery = supplierQb.bool().should(supplierQuery).createQuery();
		return supplierQuery;
	}

	public Integer getAuditStatus() {
		return auditStatus;
	}

	public void setAuditStatus(Integer auditStatus) {
		this.auditStatus = auditStatus;
	}
	
}
